package com.wnd.dell.myapplication;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<Teacher> teachers = new ArrayList<>();
        teachers.add(new Teacher("张海霞", R.mipmap.ic_launcher, "张海霞，北京大学教授国际大学生iCAN创新创业大赛发起人、主席北京大学信息科学技术学院教授全球华人微纳米分子系统学会秘书长IEEE NTC 北京分会主席。"));
        teachers.add(new Teacher("陈江", R.mipmap.ic_launcher, "陈江，北京大学信息科学技术学院副教授，高等学校电路和信号系统教学与教材研究会常务理事，中国通信理论与信号处理委员会委员。"));
        teachers.add(new Teacher("叶蔚", R.mipmap.ic_launcher, "叶蔚，北京大学软件工程国家工程研究中心副研究员，创办了技术学习服务平台天码营(http://tianmaying.com)与软件开发协同工具Onboard(http://onboard.cn)。"));



        MyAdapter myAdapter =new MyAdapter(this,teachers);
        ListView listView =(ListView) this.findViewById(R.id.teacher_listView) ;
        listView.setAdapter(myAdapter);


    }


    static class MyAdapter extends BaseAdapter
    {

        private List<Teacher> mTeacherList;
        private Context mContext;

        public MyAdapter(Context context,List<Teacher> teacherList) {
            this.mContext =context;
            this.mTeacherList =teacherList;
        }

        @Override
        public int getCount() {
            return mTeacherList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            System.out.println("getView " + position + " " + convertView);
            ViewHolder holder = null;
            if (convertView == null) {
                LayoutInflater inflater=LayoutInflater.from(mContext);
                convertView = inflater.inflate(R.layout.item_layout,null);
                holder = new ViewHolder();
//                holder.textView = (TextView)convertView.findViewById(R.id.tv_name);
//                holder.textView2 = (TextView)convertView.findViewById(R.id.tv_desc);
                holder.imageView = (ImageView)convertView.findViewById(R.id.iv_head);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.textView.setText(mTeacherList.get(position).getName());
            holder.textView2.setText(mTeacherList.get(position).getDesc());
            holder.imageView.setBackgroundResource(mTeacherList.get(position).getImageId());
            return convertView;
        }

    }

    public static class ViewHolder {
        public TextView textView;
        public TextView textView2;
        public ImageView imageView;
    }


//    LayoutInflater inflater=LayoutInflater.from(mContext);
//            convertView =inflater.inflate(R.layout.item_layout,null);
//            TextView nameTextView =(TextView) convertView.findViewById(R.id.tv_name);
//            nameTextView.setText(mTeacherList.get(position).getName());
//            TextView descTextView =(TextView) convertView.findViewById(R.id.tv_desc);
//            descTextView.setText(mTeacherList.get(position).getDesc());
//            ImageView headImageView =(ImageView) convertView.findViewById(R.id.iv_head);
//            headImageView.setImageAlpha(mTeacherList.get(position).getImageId());
//
//
//            return convertView;
//        }
//    }

}
